import 'dart:math';

import 'package:equatable/equatable.dart';

/// {@template position}
///
/// 2-dimensional position model.
///
/// (0, 0) is the top left corner of the board.
///
/// {@endtemplate}
class Position extends Equatable implements Comparable<Position> {
  /// {@macro position}
  const Position({required this.x, required this.y});

  /// The x position.
  final int x;

  /// The y position.
  final int y;

  /// Calculates the distance between two [Position]s.
  double distance(Position other) =>
      sqrt(pow(other.x - x, 2) + pow(other.y - y, 2));

  /// Generates a new [Position] generated by adding this position with the 
  /// given [position].
  Position move(Position position) =>
      Position(x: x + position.x, y: y + position.y);

  @override
  List<Object> get props => [x, y];

  @override
  int compareTo(Position other) {
    if (y < other.y) {
      return -1;
    } else if (y > other.y) {
      return 1;
    } else if (x < other.x) {
      return -1;
    } else if (x > other.x) {
      return 1;
    }

    return 0;
  }
}
